package makeeasier.air.utils
{
	import fl.controls.ScrollBar;
	import flash.desktop.NativeApplication;
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.NativeWindowBoundsEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	$(CBI)* ...
	$(CBI)* @author Sentinel
	$(CBI)*/
	public class DebugWindow extends NativeWindow
	{
		private var logTextField:TextField;
		public static var debugWindow:DebugWindow;
		private var scrollBar:ScrollBar;
		private var bg:Sprite;
		
		public function DebugWindow() 
		{
			if (debugWindow) {
				debugWindow.close();
			}
			debugWindow = this;
			
			var nwi:NativeWindowInitOptions = new NativeWindowInitOptions();
			nwi.resizable = true;
			nwi.minimizable = true;
			nwi.maximizable = false;
			nwi.systemChrome = NativeWindowSystemChrome.STANDARD;
			
			super(nwi);
			
			super.title = "log window";
			super.bounds = new Rectangle(200, 200, 800, 600);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			addEventListener(NativeWindowBoundsEvent.RESIZE, onResize);
			addEventListener(Event.ACTIVATE, onActivate);
		}
		
		private function onActivate(e:Event):void
		{
			removeEventListener(Event.ACTIVATE, onActivate);
			init();
		}
		
		/*
		 * Writes given text into window.
		*/
		public function log(str:String):void
		{
			logTextField.appendText(str);
			scrollBar.minScrollPosition = 0;
			scrollBar.maxScrollPosition = logTextField.textHeight;
			scrollBar.setScrollPosition(logTextField.textHeight);
			//scrollBar.dispatchEvent(new Event(Event.SCROLL));
		}
		
		private function init():void
		{
			bg = new Sprite();
			bg.graphics.beginFill(0x454545, 1);
			bg.graphics.drawRect(0, 0, 100, 100);
			bg.graphics.endFill();
			bg.cacheAsBitmap = true;
			
			var tf:TextFormat = new TextFormat();
			tf.font = "courier";
			tf.size = 8;
			tf.color = 0xffffff;
			
			logTextField = new TextField();
			logTextField.defaultTextFormat = tf;
			logTextField.selectable = true;
			logTextField.wordWrap = true;
			logTextField.multiline = true;
			logTextField.border = true;
			logTextField.borderColor = 0x2c2c2c;
			
			scrollBar = new ScrollBar();
			scrollBar.addEventListener(Event.SCROLL, onScroll);
			
			stage.addChild(bg);
			stage.addChild(logTextField);
			stage.addChild(scrollBar);
			
			resize();
		}
		
		private function onScroll(e:Event):void 
		{
			logTextField.scrollV = logTextField.numLines * (scrollBar.scrollPosition / scrollBar.maxScrollPosition);
		}
		
		private function resize():void
		{
			bg.x = 0;
			bg.y = 0;
			bg.width = width;
			bg.height = height;
			
			logTextField.width = width - 40;
			logTextField.height = height - 60;
			logTextField.x = 10;
			logTextField.y = 10;
			
			scrollBar.x = logTextField.width;
			scrollBar.y = 10;
			scrollBar.setSize(10, logTextField.height );
		}
		
		private function onResize(e:Event):void
		{
			resize();
		}
		
		private function onExit(e:Event):void
		{
			close();
		}
		
		
		
	}

}